<template>
  <div>
    <el-button
      type="info"
      plain
      @click="dialogForm = true"
      style="float: right; margin-right: 10%; border: 0"
      >增加</el-button
    >
    <el-dialog :close-on-click-modal ="false" 
    title="新增商品" :visible.sync="dialogForm" width="25%">
      <el-form :model="form">
        <el-form-item label="车辆名称" :label-width="formLabelWidth">
          <el-input
            v-model="form.title"
            maxlength="15"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="车辆图片" :label-width="formLabelWidth">
          <el-input
            v-model="form.lpic"
            maxlength="256"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="价钱/万" :label-width="formLabelWidth">
          <el-input
            v-model="form.gprice"
            maxlength="15"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="increase();dialogForm = false">确 定</el-button>
      </div>
    </el-dialog>

    <el-table
      :data="data"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      border
      v-if="data"
    >
      <el-table-column prop="cid" label="车辆序号" width="100px">
      </el-table-column>
      <el-table-column prop="title" label="车辆名称"> </el-table-column>
      <el-table-column  label="车辆图片" type=index width="120px">
        <template slot-scope="scope">
          <img  :src="data[scope.$index].lpic" alt="" 
          style="width:100%"/>
        </template> 
        </el-table-column>
      <el-table-column prop="gprice" label="价格 / 万"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="
              dialogFormVisible = true
              handleEdit(scope)
            "
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :close-on-click-modal ="false" title="修改的内容" 
    :visible.sync="dialogFormVisible" width="25%">
      <el-form :model="form">
        <el-form-item label="车辆图片" :label-width="formLabelWidth">
          <el-input v-model="form.lpic" maxlength="128"></el-input>
        </el-form-item>
        <el-form-item label="车辆名称" :label-width="formLabelWidth">
          <el-input v-model="form.title" maxlength="20"></el-input>
        </el-form-item>
        <el-form-item label="价格/万" :label-width="formLabelWidth">
          <el-input v-model="form.gprice" maxlength="10"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" plain @click="dialogFormVisible = false"
          >取 消</el-button
        >
        <el-button type="info" plain @click="update()">确 定</el-button>
      </div>
    </el-dialog>

    <el-button-group
      style="float: right; margin-right: 10%; border: 0; margin-top: 10px"
    >
      <el-button
        type="info"
        size="mini"
        plain
        @click="getcommodity(page--)"
        :disabled="isActive"
        >上一页</el-button
      >
      <el-button
        :disabled="disabled"
        type="info"
        size="mini"
        plain
        @click="getcommodity(page++)"
        >下一页</el-button
      >
    </el-button-group>
  </div>
</template>

<script>
import qs from "qs"
export default {
  data() {
    return {
      disabled: false,
      data: [],
      page: 1,
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        lpic: "",
        title: "",
        gprice: "",
      },
      //弹框前面的大小
      formLabelWidth: "100px",
      dialogTable: false,
      dialogForm: false,
    }
  },
  mounted() {
    this.getcommodity(1)
  },
  methods: {
    //获取
    getcommodity() {
      //所有的内容
      this.axios.get(`/get_cars?page=${this.page}`).then((res) => {
        // console.log(res.data.data)
        if (res.data.data.length < 8) {
          this.disabled = true
        }
        let data=res.data.data
        this.data=data
      })
    },
    //新增
    increase() {
      this.axios
        .post("/post_car", qs.stringify(this.form))
        .then(async (res) => {
          this.$notify({
            title: "成功",
            message: "这是一条成功的提示消息",
            type: "success",
          });
          await this.getcommodity(this.page)
        });
    },

    //修改
    handleEdit(index) {
      this.form.cid = index.row.cid
      this.img = index.row.lpic
    },
    update() {
      this.axios
        .post("/update_car", qs.stringify(this.form))
        .then(async (res) => {
          // window.location.reload();
          this.$notify({
            title: "成功",
            message: "这是一条成功的提示消息",
            type: "success",
          });
          await this.getcommodity(this.page)
        });
    },
    //删除
    handleDelete(index) {
      let cid = this.data[index].cid
      this.axios.delete(`/del_car/${cid}`).
      then(async (res) => {
        // window.location.reload()
        this.$notify({
            title: "成功",
            message: "这是一条成功的提示消息",
            type: "success",
          });
          await this.getcommodity(this.page)
      })
    },
  },
  computed: {
    //当页码数到第一页时，上一页按钮禁用
    isActive() {
      this.disabled = false
      if (this.page > 1) {
        return false
      } else {
        return true
      }
    },
  },
}
</script>
<style lang="scss" scoped></style>
